package com.yangwei.examination.user.controller;

import ch.qos.logback.classic.Logger;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yangwei.examination.teacher.pojo.School;
import com.yangwei.examination.teacher.service.SchoolService;
import com.yangwei.examination.user.pojo.Student;
import com.yangwei.examination.user.pojo.Teacher;
import com.yangwei.examination.user.pojo.User;
import com.yangwei.examination.user.service.StudentService;
import com.yangwei.examination.user.service.TeacherService;
import com.yangwei.examination.user.service.UserService;
import com.yangwei.examination.utils.ResultUtils;
import com.yangwei.examination.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
@Api(tags = "用户相关", description = "用户登录&首页数据统计")
@RestController
@RequestMapping("/user")
public class UserController {

    private final Logger log = (Logger) LoggerFactory.getLogger(UserController.class);

    @Resource
    private UserService userService;

    @Resource
    private StudentService studentService;

    @Resource
    private TeacherService teacherService;

    @Resource
    private SchoolService schoolService;

    //老师和学生登录
    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public ResultVo login( @ApiParam(name = "用户实体", value = "userName和password非空") @RequestBody User user) {

        log.info("用户 {} 登录", JSON.toJSON(user));

        QueryWrapper<Teacher> queryTea = new QueryWrapper<>();
        QueryWrapper<Student> queryStu = new QueryWrapper<>();

        queryTea.lambda().
                eq(Teacher::getUserName, user.getUserName()).
                eq(Teacher::getPassword, user.getPassword());

        queryStu.lambda().
                eq(Student::getUserName, user.getUserName()).
                eq(Student::getPassword, user.getPassword());

        Teacher teacher = teacherService.getOne(queryTea);

        Student student = studentService.getOne(queryStu);




        if (teacher != null) {
            return ResultUtils.success("登录成功", teacher);
        }
        if (student != null) {
            student.setSchool(
                    schoolService.getOne(Wrappers.lambdaQuery(School.class)
                            .eq(School::getSchoolId,student.getSchoolId())
                    )
            );
            return ResultUtils.success("登录成功", student);
        }

        return ResultUtils.error("用户名或密码错误！");

    }

    //用于统计学生人数
    @ApiOperation(value = "统计学生人数")
    @GetMapping("/getStuCount")
    public ResultVo getStudentCount(){
        int count = studentService.count();
        return ResultUtils.success("查询成功",count);
    }

    //用于统计教师人数
    @ApiOperation(value = "统计教师人数")
    @GetMapping("/getTeaCount")
    public ResultVo getTeacherCount(){
        int count = teacherService.count();
        return ResultUtils.success("查询成功",count);
    }

    //用于统计院校数量
    @ApiOperation(value = "统计院校数量")
    @GetMapping("/getSchoolCount")
    public ResultVo getSchoolCount(){
        int count = schoolService.count();
        return ResultUtils.success("查询成功",count);
    }

}
